package com.jcl.recruit.searchresume.mapper;

import com.jcl.recruit.mapper.SqlMapper;
import com.jcl.recruit.mapper.provider.SqlProvider;
import com.jcl.recruit.searchresume.model.SearchResumeRecord;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface SearchResumeRecordMapper extends SqlMapper {

    String TABLE_NAME = " search_resume_record ";
    String ORDER_BY_DESC = " order by update_time desc ";

    @Select({
            QUERY_SCRIPT_START,
            SQL_QUERY,
            QUERY_WHERE_START,
            IF_START_CORP_CODE,
            SQL_CORP_CODE,
            IF_END,
            IF_START_USER_ID,
            SQL_AND, SQL_USER_ID,
            IF_END,
            IF_START_STATUS,
            SQL_AND, SQL_STATUS,
            IF_END,
            "<if test='null!=wechatPush'>",
            " and wechat_push = #{wechatPush}",
            "</if>",
            "<if test='null!=emailPush'>",
            " and email_push = #{emailPush}",
            "</if>",
            QUERY_WHERE_END,
            ORDER_BY_DESC,
            QUERY_SCRIPT_END
    })
    List<SearchResumeRecord> findList(@Param("corpCode") String corpCode,
                                      @Param("userId") Integer userId,
                                      @Param("status") Integer status,
                                      @Param("wechatPush") Integer wechatPush,
                                      @Param("emailPush") Integer emailPush);

    @Select({
            QUERY_SCRIPT_START,
            SQL_QUERY,
            QUERY_WHERE_START,
            IF_START_CORP_CODE,
            SQL_CORP_CODE,
            IF_END,
            "<if test='null!=keyword'>",
            " and keyword = #{keyword}",
            "</if>",
            " and status = 0 ",
            QUERY_WHERE_END,
            ORDER_BY_DESC,
            QUERY_SCRIPT_END

    })
    List<SearchResumeRecord> findListByKeyword(@Param("corpCode") String corpCode,
                                               @Param("keyword") String keyword);
    @Select({
            "<script>",
            "SELECT * FROM search_resume_record where status = 0",
            "<if test=\"null != corpCode\"> AND corp_code=#{corpCode} </if>",
            "<if test=\"null != userId\"> AND user_id=#{userId} </if>",
            "<if test=\"null != keyword\"> AND keyword=#{keyword} </if>",
            "<if test=\"null != city\"> AND city=#{city} </if>",
            "<if test=\"null != education\"> AND education=#{education} </if>",
            "<if test=\"null != workYear\"> AND work_year=#{workYear} </if>",
            "<if test=\"null != wechatPush\"> AND wechat_push=#{wechatPush} </if>",
            "<if test=\"null != emailPush\"> AND email_push=#{emailPush}</if>",
            "</script>"
    })
    List<SearchResumeRecord> findListByCondition(@Param("corpCode") String corpCode,
                                                      @Param("userId") Integer userId,
                                                      @Param("keyword") String keyword,
                                                      @Param("city") String city,
                                                      @Param("education") String education,
                                                      @Param("workYear") Integer workYear,
                                                      @Param("wechatPush") Integer wechatPush,
                                                      @Param("emailPush") Integer emailPush);

    @Select({
            SQL_QUERY_ID
    })
    SearchResumeRecord findOne(String corpCode, @Param("id") Integer id);


    @InsertProvider(type = SearchResumeRecordSqlProvider.class, method = SqlProvider.SQL_METHOD_INSERT)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(SearchResumeRecord record);

    @UpdateProvider(type = SearchResumeRecordSqlProvider.class, method = SqlProvider.SQL_METHOD_UPDATE)
    int update(SearchResumeRecord record);

    @Update({
            "update search_resume_record set status = 1 where id = #{id}"
    })
    int deleteById(String corpCode, @Param("id") Integer id);

    String SQL_QUERY = "select distinct * from " + TABLE_NAME + " ";
    String SQL_QUERY_ID = "select * from " + TABLE_NAME + " where id = #{id}";

}
